package com.lxj.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * TestJdbc2
 */
public class TestJdbc2 {

    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
            //TODO: handle exception
        }

        String sql = "insert into article values(null,?)";
        try (
            Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?charactorEncoding=UTF-8", "root", "root");
            // PreparedStatement ps = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        ) {

            DatabaseMetaData dmd = c.getMetaData();
            System.out.println(dmd.getDatabaseProductName());
            System.out.println(dmd.getDatabaseProductVersion());
            System.out.println(dmd.getCatalogSeparator());
            System.out.println(dmd.getDriverVersion());

            System.out.println("可用的数据库列表：");
            ResultSet rs = dmd.getCatalogs();
            while (rs.next()) {
                System.out.println("数据库名称：\t"+rs.getString(1));
                // System.out.println("数据库名称：\t"+rs.getString(2));
            }
            // 设置参数
            // ps.setString(1, "提莫");
            // // 执行
            // ps.execute();

            // ResultSet rs = ps.getGeneratedKeys();
            // if(rs.next()) {
            //     int id = rs.getInt(1);
            //     System.out.println(id);
            // }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}